﻿using System;
using System.Data;
using System.Data.SqlClient;
using PPI.Data;
using PPI.Entities.Distribution;

namespace PPIDataAccess.Distribution
{
    public class DMeter
    {
        public static DataSet Select(string connectionKey)
        {
            SqlDataAdapter da = null;

            try
            {
                da = new SqlDataAdapter("up_tb_Vendor_Distribution_Meters_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception exc)
            {
                throw new Exception("Error executing up_tb_Vendor_Distribution_ImagesXPoles_SelectAll", exc);
            }
            finally
            {
                if (da != null)
                    da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectWithoutImages(string connectionKey)
        {
            SqlDataAdapter da = null;

            try
            {
                da = new SqlDataAdapter("up_tb_Vendor_Distribution_Meters_SelectWithoutImage", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception exc)
            {
                throw new Exception("Error executing up_tb_Vendor_Distribution_ImagesXPoles_SelectAll", exc);
            }
            finally
            {
                if (da != null)
                    da.SelectCommand.Connection.Close();
            }
        }

        public static int Insert(EMeter meter, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Meters_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Pole

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                #endregion

                #region - Latitude / Longitude -

                if (meter.Latitude == double.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, meter.Latitude));

                if (meter.Longitude == double.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, meter.Longitude));

                #endregion

                #region Number

                if (string.IsNullOrEmpty(meter.Number))
                    cmd.Parameters.Add(
                        DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(
                        DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, meter.Number));

                #endregion

                #region ImageFileName

                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@ImageFileName", 100, ParameterDirection.Input, true, DBNull.Value));

                #endregion

                #region Comments

                if (string.IsNullOrEmpty(meter.Comments))
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, meter.Comments));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionMeter_MeterId", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionMeter_MeterId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();

                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Update(EMeter meter, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Meters_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region IdMeter

                if (meter.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MeterId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MeterId", ParameterDirection.Input, true, meter.Id));

                #endregion

                #region Pole

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                if (!String.IsNullOrEmpty(meter.Image.FileName))
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MeterImage", ParameterDirection.Input, true, meter.Image.FileName));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MeterImage", ParameterDirection.Input, true, DBNull.Value));
               
                #endregion

                #region - Latitude / Longitude -

                if (meter.Latitude == double.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, meter.Latitude));

                if (meter.Longitude == double.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, meter.Longitude));

                #endregion

                #region Number

                if (string.IsNullOrEmpty(meter.Number))
                    cmd.Parameters.Add(
                        DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(
                        DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, meter.Number));

                #endregion

                #region Comments

                if (string.IsNullOrEmpty(meter.Comments))
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, meter.Comments));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionMeter_MeterId", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();

                #region Meter Notes 

                //Delete actual Meter Notes.
                cmd = new SqlCommand("up_tb_Vendor_Distribution_NotesXMeter_Delete", tr.Connection);
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;

                if (meter.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MeterId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MeterId", ParameterDirection.Input, true, meter.Id));

                cmd.ExecuteNonQuery();

                //Inserts meter notes
                foreach (ENote note in meter.Notes)
                {
                    cmd = new SqlCommand("up_tb_Vendor_Distribution_NotesXMeter_Insert", tr.Connection);
                    cmd.Transaction = tr;

                    cmd.CommandType = CommandType.StoredProcedure;

                    if (note.Id == Int32.MinValue)
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_NoteFk", ParameterDirection.Input, true, DBNull.Value));
                    else
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_NoteFk", ParameterDirection.Input, true, note.Id));

                    if (meter.Id == Int32.MinValue)
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_MeterFk", ParameterDirection.Input, true, DBNull.Value));
                    else
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_MeterFk", ParameterDirection.Input, true, meter.Id));

                    cmd.ExecuteNonQuery();
                }

                #endregion

                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Delete(Int32 meterId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Meters_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region IdMeter

                if (meterId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MeterId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MeterId", ParameterDirection.Input, true, meterId));

                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }
    }
}
